using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Holoville.HOTween;
using UnityEngine;

public class CarBehavior : EntityBehavior
{
	private struct CarWheels
	{
		public Transform rim;

		public bool isLeft;

		public bool isSteer;

		public float steerRange;
	}

	public const int DefaultCarHeight = 6;

	public Transform cameraP;

	public Vector3 lastPosition = Vector3.zero;

	[SerializeField]
	private RCC_TruckTrailer truckTrailer;

	public bool onLoop;

	public Vector3 shopScale = Vector3.one;

	public float carParticlesScaler = 1f;

	[Header("Множители урона, зависимо от зоны попадания")]
	public List<ColliderMultiplier> damageMultiplyingColliders = new List<ColliderMultiplier>();

	[HideInInspector]
	public bool onSpringboardJump;

	[HideInInspector]
	public Vector3 springboardJumpStartPos;

	public int addMoneyAfterDead = 2;

	protected BasicInput basInput;

	private List<MeshRenderer> _carRenderers;

	public GameObject hitCollider;

	public Transform onActiveCarParrent;

	public CarShadowType currentShadowType = CarShadowType.Medium;

	public visibleObjPhoton ScriptVisibleForLerp;

	private float _lastVerticalDamageTime;

	private float _lastDamageTime;

	public float ControlForShop;

	public float AccelerationForShop = -0.5f;

	public RCC_CarControllerV3 RCCcar;

	private float initialMaxSpeed;

	private float initialEngineTorque;

	private float initialSteeringHelperStrength;

	private int initialMaxHealth;

	private float initialDamage;

	private IEnumerator _wheelsTurn;

	private IEnumerator _disableCarWithDelay;

	private CarWheels[] carWheels;

	public float angularVelocityY = 1.2f;

	public bool isFlaing;

	private Vector3 lastPositionOnline = Vector3.zero;

	private Vector3 lastDirectionOnline = Vector3.zero;

	public float currentSpeedOnline;

	public float currentDirectionOnline;

	public float currentSteerDirectionOnline;

	private Vector3 screenCenter = new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f);

	private float aimScaleParam = 1f;

	private float maxScale = 2f;

	public LayerMask aimLayerMask;

	private float aimTime = 25f;

	public Vector3 aimVelocity = Vector3.zero;

	public GameObject AimObjectDynamic;

	public UILabel damageLabel;

	private UITexture AimObjectDynamicTexture;

	private GameObject AimObjectStatic;

	public Animator[] deerAnimators;

	public float forcedBreakMult = 250f;

	private bool applyDownForce = true;

	public float downForceMaxDist = 1.1f;

	private float downMult = 225f;

	private ForceMode downForceMode;

	private float currentBoostTrailTime;

	private bool needBlinking;

	private List<BlinkingTrigger> blinkingTriggers;

	private Coroutine blinkingIE;

	private Material blinkingMaterial;

	public float continuousAmount = 500f;

	public float firstBoost = 5000f;

	public float boostTrailsLerp = 2f;

	public List<TrailRenderer> boostTrails;

	public List<ParticleSystem> boostParticles;

	public float jumpForce = 5000f;

	public float sniperModZoom = 10f;

	public float tankRotationSpeed = 1f;

	public List<MeshRenderer> carRenderers
	{
		get
		{
			if (_carRenderers == null)
			{
				_carRenderers = new List<MeshRenderer>();
				MeshRenderer[] componentsInChildren = GetComponentsInChildren<MeshRenderer>();
				foreach (MeshRenderer meshRenderer in componentsInChildren)
				{
					if (meshRenderer.tag != "Player")
					{
						_carRenderers.Add(meshRenderer);
					}
				}
			}
			return _carRenderers;
		}
	}

	public bool showAfterSit
	{
		get
		{
			return sitPoints[0].showAfterSit;
		}
	}

	public bool IsBlinking
	{
		get
		{
			return needBlinking || blinkingIE != null;
		}
	}

	private List<BlinkingTrigger> BlinkingTriggers
	{
		get
		{
			if (blinkingTriggers == null)
			{
				CollectCollider();
			}
			return blinkingTriggers;
		}
	}

	public override void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Loop")
		{
			TPSCamera.instance.onLoop = true;
		}
		if (other.tag == "pointExitCar")
		{
			applyDownForce = false;
			Invoke("ReEnableDownForce", 0.75f);
		}
	}

	public void ToggleRenderers(bool enable)
	{
		foreach (MeshRenderer carRenderer in carRenderers)
		{
			carRenderer.enabled = enable;
		}
	}

	public override int GetBodyHeight()
	{
		return (base.BodyHeight != 0) ? base.BodyHeight : 6;
	}

	protected override void OnCollisionEnter(Collision collision)
	{
		if (base.isMine && RCCcar.speed > 20f && collision.gameObject.tag != "ground" && collision.gameObject.tag != "Weapon" && !collision.contacts[0].otherCollider.transform.gameObject.name.Contains("road") && !collision.contacts[0].otherCollider.transform.gameObject.name.Contains("asphalt") && !collision.contacts[0].otherCollider.transform.gameObject.name.Contains("Tramp") && !collision.contacts[0].otherCollider.transform.gameObject.name.Contains("asfalt"))
		{
			float num = TPSCamera.SignedAngle(base.transform.forward, collision.contacts[0].normal, Vector3.up);
			if (num >= 0f)
			{
				angularVelocityY = Mathf.Abs(angularVelocityY);
			}
			else if (angularVelocityY > 0f)
			{
				angularVelocityY *= -1f;
			}
			rigidCar.angularVelocity = new Vector3(rigidCar.angularVelocity.x, angularVelocityY, rigidCar.angularVelocity.z);
		}
		SpringboardJumpLanding();
		base.OnCollisionEnter(collision);
	}

	private void SpringboardJumpLanding()
	{
		if (!onSpringboardJump)
		{
			return;
		}
		onSpringboardJump = false;
		if (!settings.offlineMode)
		{
			if (base.photonView.isMine)
			{
				float num = Vector3.Distance(springboardJumpStartPos, base.transform.position);
				GameplayEventsManager.instance.NewDrivingEvent(num, this, false);
				AllUnitaryAchievements.instance.springboardJump_50.Increment();
				if (num > 50f)
				{
					AllUnitaryAchievements.instance.springboardCrazyJump_20.Increment();
				}
			}
		}
		else
		{
			float num2 = Vector3.Distance(springboardJumpStartPos, base.transform.position);
			AllUnitaryAchievements.instance.springboardJump_50.Increment();
			if (num2 > 50f)
			{
				AllUnitaryAchievements.instance.springboardCrazyJump_20.Increment();
			}
		}
	}

	public override void Awake()
	{
		if ((bool)CarInShopManager.instance && CarInShopManager.instance.isShopCar(this))
		{
			return;
		}
		if (carWithWeapon && startCountBulletCar == 0)
		{
			if (privateCar)
			{
				startCountBulletCar = 20;
			}
			else
			{
				startCountBulletCar = 15;
			}
		}
		base.Awake();
		SwitchTexture(GetCurrentSkin());
		basInput = GetComponent<BasicInput>();
		initialPosition = base.transform.position;
		initialRotation = base.transform.rotation;
		if (ParentForAllCar.instance != null)
		{
			onActiveCarParrent = ParentForAllCar.instance.transform;
		}
		else
		{
			onActiveCarParrent = base.transform.parent;
		}
		if (base.transform.parent != null)
		{
			initialParent = base.transform.parent;
		}
		else
		{
			initialParent = base.transform;
		}
		ScriptVisibleForLerp = GetComponentInChildren<visibleObjPhoton>();
		RCCcar = GetComponent<RCC_CarControllerV3>();
		if (!privateCar)
		{
			respawnCar = true;
		}
		else
		{
			base.transform.SetParent(onActiveCarParrent);
		}
		if (smokePoint != null)
		{
			smokePoint.gameObject.SetActive(true);
		}
		if (explosionPoint != null)
		{
			explosionPoint.gameObject.SetActive(true);
		}
		HOTween.Init();
		Enable();
		Disable();
		if (carWithWeapon && RCCcar.vehicleType != VehicleType.Boat && GameTopWindow.instance != null)
		{
			AimObjectDynamic = CarWindow.instance.TankAimDynamic;
			damageLabel = CarWindow.instance.damageLabel;
			AimObjectDynamicTexture = AimObjectDynamic.GetComponent<UITexture>();
			AimObjectStatic = CarWindow.instance.TankAimStatic;
		}
		initialEngineTorque = RCCcar.engineTorque;
		initialMaxHealth = maxHealth;
		initialMaxSpeed = RCCcar.maxspeed;
		initialSteeringHelperStrength = RCCcar.steerHelperStrength;
		if (base.vehicleWeapon != null)
		{
			initialDamage = base.vehicleWeapon.Damage;
		}
	}

	protected override void UpdateAmo()
	{
		base.UpdateAmo();
		if (base.isMine)
		{
			CarWindow.countCarBullets = base.countBulletCar;
		}
	}

	[PunRPC]
	public override void Kill(int iKillerId = -9999)
	{
		if (!isDead)
		{
			base.Kill(iKillerId);
			onSpringboardJump = false;
			ActivateCar(false);
		}
	}

	public override void Start()
	{
		base.Start();
	}

	public override void ApplyCarUpgradesRPC(string carName)
	{
		float carUpgradeCoef = MarketController.Instance.GetCarUpgradeCoef(carName, UpgradeType.Body);
		float carUpgradeCoef2 = MarketController.Instance.GetCarUpgradeCoef(carName, UpgradeType.Engine);
		float carUpgradeCoef3 = MarketController.Instance.GetCarUpgradeCoef(carName, UpgradeType.Intake);
		float carUpgradeCoef4 = MarketController.Instance.GetCarUpgradeCoef(carName, UpgradeType.Wheels);
		ProductVehicleSkin equipedVehicleSkin = MarketController.Instance.GetEquipedVehicleSkin(carName);
		string text = ((equipedVehicleSkin != null && !(equipedVehicleSkin.skinTextureName == string.Empty)) ? equipedVehicleSkin.skinTextureName : string.Empty);
		if (PhotonNetwork.connected)
		{
			base.photonView.RPC("ApplyCarUpgrades", PhotonTargets.All, carUpgradeCoef2, carUpgradeCoef3, carUpgradeCoef, carUpgradeCoef4, text);
			PhotonNetwork.player.UpdateProperty(PlayerPropsKeys.carSkin, text);
		}
		else
		{
			ApplyCarUpgrades(carUpgradeCoef2, carUpgradeCoef3, carUpgradeCoef, carUpgradeCoef4, text);
		}
	}

	[PunRPC]
	public void ApplyCarUpgrades(float maxSpeedCoef, float engineTorqueCoef, float maxHealthCoef, float steeringHelperStrengthCoef, string newSkin)
	{
		RCCcar.maxspeed = initialMaxSpeed * maxSpeedCoef;
		if (RCCcar.vehicleType == VehicleType.Tank)
		{
			base.vehicleWeapon.damage = initialDamage * engineTorqueCoef;
		}
		else
		{
			RCCcar.engineTorque = initialEngineTorque * engineTorqueCoef;
		}
		maxHealth = (int)((float)initialMaxHealth * maxHealthCoef);
		RCCcar.steerHelperStrength = Mathf.Clamp01(initialSteeringHelperStrength * steeringHelperStrengthCoef);
		Texture texture = ((!(newSkin == string.Empty)) ? possibleCarTextures.Where((Texture x) => x != null).FirstOrDefault((Texture x) => x.name == newSkin) : textCar);
		if (!isDead)
		{
			SwitchTexture(texture, true);
		}
		textCar = ((!(texture == null)) ? texture : textCar);
		if (base.isMine)
		{
			PrivateCarManager.instance.slots[0].LoadAndAddTexture(this);
		}
	}

	public override float GetCurrentSpeed()
	{
		return RCCcar.speed;
	}

	public override float GetMaxSpeed()
	{
		return RCCcar.maxspeed;
	}

	public float GetCurrentDirection()
	{
		return RCCcar.direction;
	}

	public override void OnDisable()
	{
		lastPosition = Vector3.zero;
		if (carWithWeapon)
		{
			settings.speedCarForIgnoreEnemy = predSpeedCarForIgnoreEnemy;
		}
		if (RCCcar.tankTower != null)
		{
			RCCcar.tankTower.UnSubscribe();
		}
	}

	public override void ActivateCar(bool val, int playerId = -1, bool dropPlayer = false)
	{
		if (RCCcar != null)
		{
			RCCcar.EngineToggle(val);
		}
		if (dropPlayer)
		{
			clearSitPoint();
		}
		if (_disableCarWithDelay != null && base.enabled)
		{
			StopCoroutine(_disableCarWithDelay);
			_disableCarWithDelay = null;
		}
		setIconMiniMap(val);
		if (val)
		{
			GetControll(playerId);
			Enable();
			UpdateAmo();
			if (!base.isMine)
			{
				StartCoroutine(OnlineMovment());
			}
		}
		else if (base.isMine)
		{
			rigidCar.angularVelocity = Vector3.zero;
			_disableCarWithDelay = DisableWithDelay();
			if (base.gameObject.GetActive())
			{
				StartCoroutine(_disableCarWithDelay);
			}
			else
			{
				Disable();
			}
		}
	}

	private void ToggleTankArrows(bool turnOn)
	{
		if (RCCcar.vehicleType == VehicleType.Tank && turnOn)
		{
			GameController.instance.tankArrowsController.transform.SetParent(base.transform);
			GameController.instance.tankArrowsController.transform.localPosition = Vector3.zero;
			GameController.instance.tankArrowsController.transform.localScale = Vector3.one;
			GameController.instance.tankArrowsController.transform.localRotation = Quaternion.identity;
			GameController.instance.tankArrowsController.carController = RCCcar;
			GameController.instance.tankArrowsController.gameObject.SetActive(true);
			GameController.instance.tankArrowsController.Init();
		}
		else
		{
			GameController.instance.tankArrowsController.carController = null;
			GameController.instance.tankArrowsController.gameObject.SetActive(false);
		}
	}

	public override Transform GetIn(Humanoid player, int n = -1)
	{
		base.transform.parent = onActiveCarParrent;
		onSpringboardJump = false;
		Transform @in = base.GetIn(player, n);
		ActivateCar(true, player.viewId);
		if (settings.offlineMode || player.photonView.isMine)
		{
			ToggleTankArrows(true);
			GameController.instance.switchInterface(true);
			UpdateAmo();
			if (RCCcar.tankTower != null)
			{
				RCCcar.tankTower.Subscribe();
			}
		}
		return @in;
	}

	public override Transform GetOut(Humanoid player, bool withAnim)
	{
		if (carWithWeapon)
		{
			setCountBullets(base.countBulletCar);
		}
		base.transform.parent = onActiveCarParrent;
		onSpringboardJump = false;
		Transform @out = base.GetOut(player, withAnim);
		ActivateCar(false);
		if (settings.offlineMode || player.photonView.isMine)
		{
			ToggleTankArrows(false);
			ToggleRenderers(true);
		}
		return @out;
	}

	public override void Enable()
	{
		isStopDrive = false;
		base.Enable();
		if (!settings.offlineMode && !base.photonView.isMine)
		{
			if (GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.RocketLeague)
			{
				RCCcar.ToggleVehicleComponents(true);
			}
			RCCcar.ToggleOnlineWheels(true);
		}
		else if (settings.offlineMode || base.photonView.isMine)
		{
			if (GetComponent<carDamageEnemy>() == null)
			{
				base.gameObject.AddComponent<carDamageEnemy>();
			}
			StopCoroutine(DisableRCCcarWithDelay());
			RCCcar.ToggleVehicleComponents(true);
			RCCcar.handbrakeInput = 0f;
			basInput.enabled = true;
			base.enabled = true;
			CancelInvoke("reset");
		}
	}

	public override void Disable()
	{
		isStopDrive = true;
		if (GameController.instance.curTypeGame == TypeGame.RocketLeague)
		{
			return;
		}
		base.Disable();
		RCCcar.EngineToggle(false);
		if (carWithWeapon)
		{
			if ((bool)AimObjectDynamic)
			{
				AimObjectDynamic.SetActive(false);
			}
			if ((bool)AimObjectStatic)
			{
				AimObjectStatic.SetActive(false);
			}
		}
		EnableNawMesh(true);
		enableSound(false);
		if (base.gameObject.activeSelf)
		{
			StartCoroutine(DisableRCCcarWithDelay());
		}
		else
		{
			if (base.isMine && !privateCar)
			{
				GetControll(-1);
			}
			RCCcar.ToggleVehicleComponents(false);
		}
		basInput.enabled = false;
		if ((settings.offlineMode || base.photonView.isMine) && base.isMine)
		{
			Object.Destroy(base.gameObject.GetComponent<carDamageEnemy>());
		}
		RCCcar.ToggleOnlineWheels(false);
	}

	public override void EnablePrivatCar()
	{
		RCCcar.EnableCar(false);
	}

	private IEnumerator DisableRCCcarWithDelay()
	{
		yield return new WaitForSeconds(0.5f);
		if (RCCcar.CanDisable())
		{
			RCCcar.ToggleVehicleComponents(false);
			if (base.isMine && !privateCar)
			{
				GetControll(-1);
			}
		}
	}

	private IEnumerator DisableWithDelay()
	{
		RCCcar.handbrakeInput = 1f;
		RCCcar.fuelInput = 0f;
		RCCcar.gasInput = 0f;
		while (true)
		{
			rigidCar.velocity *= 0.99f;
			if (RCCcar.vehicleType != VehicleType.Boat)
			{
				if (_disableCarWithDelay != null && GetCurrentSpeed() < minSpeedToStop)
				{
					if (settings.offlineMode && base.isMine)
					{
						StopDrive();
					}
					else if (!settings.offlineMode && base.isMine)
					{
						base.photonView.RPC("StopDrive", PhotonTargets.Others, null);
						Disable();
					}
					break;
				}
			}
			else if (_disableCarWithDelay != null && GetCurrentSpeed() < minSpeedToStop)
			{
				if (settings.offlineMode && base.isMine)
				{
					StopDrive();
				}
				else if (!settings.offlineMode && base.isMine)
				{
					base.photonView.RPC("StopDrive", PhotonTargets.Others, null);
					Disable();
				}
				break;
			}
			yield return null;
		}
		yield return null;
	}

	[PunRPC]
	public virtual void StopDrive()
	{
		Disable();
	}

	private void disableCarWithDeleyInSky()
	{
		if (_disableCarWithDelay != null && GetCurrentSpeed() < minSpeedToStop)
		{
			if (settings.offlineMode && base.isMine)
			{
				StopDrive();
			}
			else if (!settings.offlineMode && base.isMine)
			{
				base.photonView.RPC("StopDrive", PhotonTargets.Others, null);
				Disable();
			}
		}
		else
		{
			StartCoroutine(_disableCarWithDelay);
		}
	}

	public override bool setIconMiniMap(bool val)
	{
		if (MapWindow.instance == null)
		{
			return false;
		}
		if (iconMiniMap == null)
		{
			BCWDebug.Log("iconMiniMap is null, " + base.gameObject.name);
			return false;
		}
		if (val && GameController.instance.playerScript != null && GameController.instance.playerScript.currentCar == this)
		{
			iconMiniMap.enabled = false;
			return false;
		}
		iconMiniMap.enabled = true;
		if (val)
		{
			if (GameController.instance.curTypeGame == TypeGame.TDM || GameController.instance.curTypeGame == TypeGame.WalkingRobots)
			{
				SetIconAccordingToTeam();
			}
			else if (GameController.instance.curTypeGame == TypeGame.tanks)
			{
				CoroutineRunner.Instance.StartCoroutine(ISetIconAccordingToTeamTank());
			}
			else if (carWithWeapon)
			{
				iconMiniMap.SetType("tankWithPlayer");
			}
			else if (GameController.instance.curTypeGame == TypeGame.race)
			{
				SetIconAccordingToRace();
			}
			else if (GameController.instance.curTypeGame == TypeGame.Zombe)
			{
				iconMiniMap.SetType("FriendTeam");
			}
			else
			{
				iconMiniMap.SetType("CarWithPlayer");
			}
		}
		else if (carWithWeapon)
		{
			iconMiniMap.SetType("tank");
		}
		else if (privateCar)
		{
			iconMiniMap.SetType("car");
		}
		if (privateCar)
		{
			if (justInstantiated)
			{
				iconMiniMap.enabled = false;
			}
			else
			{
				iconMiniMap.enabled = true;
			}
			if (!val && !base.isMine)
			{
				iconMiniMap.enabled = false;
			}
		}
		if (!val && PrivateCarManager.instance != null && PrivateCarManager.instance.currentCar == this)
		{
			iconMiniMap.SetType("MyCar");
		}
		if (!privateCar)
		{
			iconMiniMap.enabled = val;
		}
		return val;
	}

	private void SetIconAccordingToTeam()
	{
		PlayerBehavior playerBehavior = getMainHuman() as PlayerBehavior;
		if (playerBehavior == null)
		{
			BCWDebug.LogError("LOG: Trying to set icon for car but there is no driver in that moment.");
		}
		else if (PlayerBehavior.IsTeammate(playerBehavior))
		{
			iconMiniMap.SetType("FriendTeam");
		}
		else
		{
			iconMiniMap.SetType("EnemyTeam");
		}
	}

	private IEnumerator ISetIconAccordingToTeamTank()
	{
		while (PlayerBehavior.MyPlayer.localTeam == PunTeams.Team.none)
		{
			yield return new WaitForSeconds(1f);
		}
		yield return null;
		if (PlayerBehavior.IsTeammate(GameController.instance.FindPlayer(base.photonView.ownerId)))
		{
			iconMiniMap.SetType("FriendTankTeam");
		}
		else
		{
			iconMiniMap.SetType("EnemyTeam");
		}
	}

	private void SetIconAccordingToTeamTank()
	{
		if (PlayerBehavior.IsTeammate(GameController.instance.FindPlayer(base.photonView.ownerId)))
		{
			iconMiniMap.SetType("FriendTankTeam");
		}
		else
		{
			iconMiniMap.SetType("EnemyTeam");
		}
	}

	private void SetIconAccordingToRace()
	{
		iconMiniMap.SetType("CarWithPlayer");
	}

	public void SetCarFromProps(Vector3 position, Quaternion rotation)
	{
		SetPrivateCarPosition(position, rotation);
	}

	[PunRPC]
	public void SetPrivateCarPosition(Vector3 pos, Quaternion rot)
	{
		base.transform.position = pos;
		base.transform.rotation = rot;
	}

	[PunRPC]
	public override void reset()
	{
		base.reset();
		onSpringboardJump = false;
		RCCcar.steerAngle = 0f;
		if (truckTrailer != null)
		{
			truckTrailer.Respawn();
		}
		if ((bool)RCCcar.tankTower)
		{
			RCCcar.tankTower.Reset();
		}
	}

	public void resetCarEnabled(bool val)
	{
		if (val)
		{
			respawnCar = true;
			return;
		}
		respawnCar = false;
		stopResetWithDelay();
	}

	public void UpdateGroundType()
	{
		isFlaing = true;
		WheelHit hit;
		RCCcar.FrontLeftWheelCollider.wheelCollider.GetGroundHit(out hit);
		if (hit.collider != null)
		{
			isFlaing = false;
		}
		RCCcar.RearRightWheelCollider.wheelCollider.GetGroundHit(out hit);
		if (hit.collider != null)
		{
			isFlaing = false;
		}
		if (RCCcar.FrontLeftWheelCollider.groundHit.collider != null)
		{
			string text = RCCcar.FrontLeftWheelCollider.groundHit.collider.tag;
			int layer = RCCcar.FrontLeftWheelCollider.groundHit.collider.gameObject.layer;
			switch (text)
			{
			case "Dno":
				inWater = true;
				break;
			default:
				inWater = false;
				break;
			}
			int num = layer;
			if (num == 11)
			{
				inWater = true;
			}
			else
			{
				inWater = false;
			}
		}
	}

	public override void OnEnable()
	{
		lastPosition = base.transform.position;
	}

	private IEnumerator OnlineMovment()
	{
		while (true)
		{
			currentDirectionOnline = Mathf.Sign(Vector3.Dot(base.transform.forward, base.transform.position - lastPositionOnline));
			currentSpeedOnline = Vector3.Distance(lastPositionOnline, base.transform.position);
			if (Vector3.Dot(base.transform.forward, Vector3.forward) > 0f)
			{
				currentSteerDirectionOnline = base.transform.forward.x - lastDirectionOnline.x;
			}
			else
			{
				currentSteerDirectionOnline = (base.transform.forward.x - lastDirectionOnline.x) * -1f;
			}
			lastPositionOnline = base.transform.position;
			lastDirectionOnline = base.transform.forward;
			yield return new WaitForSeconds(0.2f);
		}
	}

	private Vector3 WorldToUI(Vector3 position)
	{
		Vector3 result = PointerController.instance.mainCamera.WorldToScreenPoint(position);
		if (result.z < 0f)
		{
			result.x *= -1f;
			result.y *= -1f;
		}
		result -= screenCenter;
		result *= (float)PointerController.instance.uiRoot.activeHeight / (float)Screen.height;
		result.z = 0f;
		return result;
	}

	private void ShowAim()
	{
		if (AimObjectDynamic == null)
		{
			return;
		}
		Color color = Color.white;
		float num = 100f;
		damageLabel.text = string.Empty;
		if (!isFree && carWithWeapon && base.isMine && objForDirectionShoot != null)
		{
			Ray ray = new Ray(objForDirectionShoot.transform.position, objForDirectionShoot.transform.forward);
			RaycastHit hitInfo;
			Vector3 vector;
			if (Physics.Raycast(ray, out hitInfo, base.vehicleWeapon.maxDistance, aimLayerMask))
			{
				vector = hitInfo.point;
				Mortal componentInParent = hitInfo.collider.GetComponentInParent<Mortal>();
				if (componentInParent != null && !componentInParent.isDead && componentInParent != this && componentInParent is CarBehavior)
				{
					CarBehavior carBehavior = componentInParent as CarBehavior;
					if (carBehavior.Type == CarType.Tank && !carBehavior.isFree && (settings.offlineMode || !PlayerBehavior.IsTeammate(GameController.instance.FindPlayer(carBehavior.photonView.ownerId))))
					{
						num = componentInParent.UpdateDamageAccordingToHitPoint(100, hitInfo.point);
						color = ((num > 100f) ? Color.red : ((!(num < 100f)) ? Color.yellow : Color.green));
						damageLabel.text = num + "%";
					}
				}
			}
			else
			{
				vector = objForDirectionShoot.transform.position + objForDirectionShoot.transform.forward * 100f;
			}
			AimObjectDynamic.SetActive(true);
			AimObjectStatic.SetActive(true);
			float num2 = 0f;
			num2 = Vector3.Distance(objForDirectionShoot.transform.position, vector);
			if (num2 > TPSCamera.instance.rayLenght)
			{
				vector = objForDirectionShoot.transform.position + objForDirectionShoot.transform.forward * TPSCamera.instance.rayLenght;
			}
			Vector3 vector2 = WorldToUI(vector);
			Vector3 b = new Vector3(Mathf.Round(vector2.x), Mathf.Round(vector2.y), Mathf.Round(vector2.z));
			AimObjectDynamic.transform.localPosition = Vector3.Lerp(AimObjectDynamic.transform.localPosition, b, Time.deltaTime * aimTime);
			aimScaleParam = Mathf.Lerp(aimScaleParam, Mathf.Max(1f, maxScale * (Mathf.Clamp(RCCcar.speed, 0f, 30f) / 30f)), Time.deltaTime * 2f);
			weaponSpread = aimScaleParam * weaponSpreadParam;
			AimObjectDynamic.transform.localScale = Vector3.Lerp(AimObjectDynamic.transform.localScale, Vector3.one * aimScaleParam, Time.deltaTime * aimTime);
			AimObjectDynamicTexture.color = color;
		}
		else
		{
			AimObjectDynamic.SetActive(false);
			AimObjectStatic.SetActive(false);
		}
	}

	public override void ResetWeaponSpred()
	{
		if (aimScaleParam < maxScale)
		{
			aimScaleParam = maxScale;
		}
	}

	public void ForcedBrake()
	{
		if (RCCcar.direction != -1)
		{
			Vector3 vector = -base.transform.forward * RCCcar.speed * forcedBreakMult;
			rigidCar.AddForce(vector * 2f, downForceMode);
		}
	}

	private void ReEnableDownForce()
	{
		CoroutineRunner.Instance.StartCoroutine(WaitForLanding());
	}

	private IEnumerator WaitForLanding()
	{
		while (isFlaing)
		{
			yield return null;
		}
		applyDownForce = true;
	}

	public virtual void Update()
	{
		if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.race && applyDownForce)
		{
			Vector3 position = base.transform.position;
			position.y += 1f;
			RaycastHit hitInfo = default(RaycastHit);
			Ray ray = new Ray(position, -Vector3.up);
			if (Physics.Raycast(ray, out hitInfo, 100f) && hitInfo.distance > downForceMaxDist)
			{
				Vector3 vector = -Vector3.up * RCCcar.speed * downMult;
				rigidCar.AddForceAtPosition(vector * 0.5f, base.transform.position + base.transform.forward * 1.5f, downForceMode);
				rigidCar.AddForce(vector * 2f, downForceMode);
			}
		}
		if (Input.GetKey(KeyCode.R))
		{
			rigidCar.angularVelocity = new Vector3(0f, angularVelocityY, 0f);
		}
		Animator[] array = deerAnimators;
		foreach (Animator animator in array)
		{
			if (base.isMine)
			{
				animator.SetFloat("SpeedStraight", RCCcar.speed / 30f);
			}
			else
			{
				animator.SetFloat("SpeedStraight", currentSpeedOnline);
			}
		}
		UpdateGroundType();
		if (base.isMine && inWater && !respawnCar)
		{
			ResetCarWithDelay(20f);
		}
		if (!base.isMine && !isFree && base.photonView != null && getMainHuman() != null && base.photonView.ownerId != getMainHuman().ownerId)
		{
			GetControll(getMainHuman());
			if (base.isMine)
			{
				Enable();
			}
		}
		if (settings.offlineMode || (!isFree && base.photonView.isMine))
		{
			if (!isFree)
			{
				if (lastPosition != Vector3.zero && RCCcar.vehicleType != VehicleType.Boat)
				{
					GameController.instance.playerScript.traveledDistanceInCar += Vector3.Distance(base.transform.position, lastPosition);
				}
				if (GameController.instance.playerScript.traveledDistanceInCar >= 500f)
				{
					AllUnitaryAchievements.instance.TravelInCar_100000.Increment((int)GameController.instance.playerScript.traveledDistanceInCar);
					GameController.instance.playerScript.traveledDistanceInCar = 0f;
				}
				lastPosition = base.transform.position;
			}
			if (settings.learningCompleted)
			{
				if (inWater)
				{
					SpringboardJumpLanding();
					GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity * 0.7f;
				}
				if (settings.offlineMode)
				{
					if (GetCurrentSpeed() < settings.speedCarForIgnoreEnemy && !navMeshCollider.enabled)
					{
						navMeshCollider.enabled = true;
					}
					if (GetCurrentSpeed() >= settings.speedCarForIgnoreEnemy && navMeshCollider.enabled)
					{
						navMeshCollider.enabled = false;
					}
				}
			}
		}
		if (!base.isMine || boostTrails.Count < 1)
		{
			return;
		}
		float num = 0f;
		if (onBoost)
		{
			if (boostTrails[0] != null)
			{
				boostTrails[0].transform.parent.gameObject.SetActive(true);
			}
			else if (boostParticles[0] != null)
			{
				boostParticles[0].transform.parent.gameObject.SetActive(true);
			}
			num = 0.25f;
			foreach (ParticleSystem boostParticle in boostParticles)
			{
				if (!boostParticle.isPlaying)
				{
					boostParticle.Play();
				}
			}
		}
		else
		{
			num = 0f;
			foreach (ParticleSystem boostParticle2 in boostParticles)
			{
				if (boostParticle2 != null && !boostParticle2.isStopped)
				{
					boostParticle2.Stop();
				}
			}
		}
		currentBoostTrailTime = Mathf.Lerp(currentBoostTrailTime, num, Time.deltaTime * boostTrailsLerp);
		boostTrails.ForEach(delegate(TrailRenderer trail)
		{
			trail.time = currentBoostTrailTime;
		});
	}

	private void LateUpdate()
	{
		if (getMainHuman() == PlayerBehavior.MyPlayer)
		{
			ShowAim();
		}
	}

	public void HideCar()
	{
		if (settings.offlineMode)
		{
			HideCarOnline();
		}
		else if (base.photonView.isMine)
		{
			base.photonView.RPC("HideCarOnline", PhotonTargets.All);
		}
	}

	[PunRPC]
	public virtual void HideCarOnline()
	{
		BCWDebug.LogError("HideCarOnline CarBehavior");
		base.transform.position = new Vector3(0f, -1000f, 0f);
	}

	public void StartBlinking()
	{
		needBlinking = true;
		if (blinkingIE == null)
		{
			blinkingIE = StartCoroutine(Blinking());
		}
	}

	public void StopBlinking()
	{
		needBlinking = false;
	}

	private void CollectCollider()
	{
		blinkingTriggers = new List<BlinkingTrigger>();
		Collider[] componentsInChildren = GetComponentsInChildren<Collider>();
		Collider[] array = componentsInChildren;
		foreach (Collider collider in array)
		{
			if (collider is BoxCollider || collider is MeshCollider)
			{
				BlinkingTriggers.Add(collider.gameObject.AddComponent<BlinkingTrigger>());
				BlinkingTriggers[BlinkingTriggers.Count - 1].SetCollider(collider);
			}
		}
	}

	private IEnumerator Blinking()
	{
		GetComponent<Rigidbody>().isKinematic = true;
		Renderer[] renderer = GetComponentsInChildren<Renderer>(true);
		if (renderer.Length == 0)
		{
			yield break;
		}
		if (blinkingMaterial == null)
		{
			for (int j = 0; j < renderer.Length; j++)
			{
				if (renderer[j].gameObject.tag != "Weapon" && renderer[j].gameObject.layer != 12 && renderer[j].gameObject.layer != 29)
				{
					blinkingMaterial = renderer[j].sharedMaterial;
					break;
				}
			}
			blinkingMaterial.SetFloat("_Alpha", 1f);
		}
		for (int i = 0; i < renderer.Length; i++)
		{
			if (renderer[i].gameObject.tag != "Weapon" && renderer[i].gameObject.layer != 12 && renderer[i].gameObject.layer != 29)
			{
				renderer[i].sharedMaterial = blinkingMaterial;
			}
		}
		BlinkingTriggers.ForEach(delegate(BlinkingTrigger t)
		{
			t.SetTriggerActive(true);
		});
		while ((PVPManager.instance.gameIsStarted || (RaceController.instance != null && RaceController.instance.enableMove)) && (needBlinking || !BlinkingTriggers.TrueForAll((BlinkingTrigger t) => t.NotEnteredCar)))
		{
			if (GameController.instance.curTypeGame != TypeGame.RocketLeague)
			{
				yield return new WaitForSeconds(0.5f);
				blinkingMaterial.SetFloat("_Alpha", 0.3f);
				yield return new WaitForSeconds(0.5f);
				blinkingMaterial.SetFloat("_Alpha", 0.7f);
			}
			else
			{
				yield return new WaitForSeconds(0.5f);
			}
		}
		if (blinkingMaterial != null)
		{
			blinkingMaterial.SetFloat("_Alpha", 1f);
		}
		GetComponent<Rigidbody>().isKinematic = false;
		BlinkingTriggers.ForEach(delegate(BlinkingTrigger t)
		{
			t.SetTriggerActive(false);
		});
		blinkingIE = null;
	}
}
